import BaseAnimation from "./BaseAnimation";

const {ccclass, property} = cc._decorator;

@ccclass
export default class DragonComponent extends BaseAnimation {


    @property({type:dragonBones.DragonBonesAsset})
    bonesAsset: dragonBones.DragonBonesAsset = null;

    @property({type:dragonBones.DragonBonesAtlasAsset})
    bonesAtlasAsset: dragonBones.DragonBonesAtlasAsset = null;


    protected animation:dragonBones.ArmatureDisplay;

    private _armature:any;


    start () {
        if(this.initFlag){
            return;
        }
        this.init();
    }

    setBonesAsset(bones:dragonBones.DragonBonesAsset ){
        this.bonesAsset = bones;
    }

    setAtlas(atlas:dragonBones.DragonBonesAtlasAsset){
        this.bonesAtlasAsset = atlas;
    }

    init(){
        this.initFlag = true;
        if(!this.animation){
            this.animation = this.addComponent(dragonBones.ArmatureDisplay)
            this.animation.dragonAsset = this.bonesAsset;
            this.animation.dragonAtlasAsset = this.bonesAtlasAsset;
            this.animation.buildArmature('armature',this.node);
            this.animation.addEventListener(dragonBones.EventObject.COMPLETE,this.onFinished,this);            
            // dragonBones.WorldClock.clock.add(this._armature);
            this._armature = this.animation.armature;
            if(this.autoPlay){
                this.play(this.actionName,this.count);
            }
        }
    }



    play(name:string,count:number = -1){
      
        count = count == -1? 0:count;
        // cc.log(' name ',name ,' count ',count)
        this.animation.playAnimation(name,count);
    }

    onDestroy(){
        this.animation.removeEventListener(dragonBones.EventObject.COMPLETE,this.onFinished,this);          
        // dragonBones.WorldClock.clock.remove(this._armature);
        this._armature.dispose();        
    }

    // update (dt) {}
}
